package com.hwadee.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwadee.property.dto.HousePersonal;
import com.hwadee.property.mapper.HouseMapper;
import com.hwadee.property.pojo.Building;
import com.hwadee.property.pojo.House;
import com.hwadee.property.service.IBuildingService;
import com.hwadee.property.service.ICommunityService;
import com.hwadee.property.service.IHouseService;
import com.hwadee.property.dto.PageResult;
import com.hwadee.property.util.ErrorCodes;
import com.hwadee.property.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 房屋信息服务实现类
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private IBuildingService buildingService;

    @Override
    public PageResult<House> getPage(int current, int size,
                                     Long buildingId, Long communityId, Long ownerId, Integer floor, Integer roomNum,
                                     String roomNumber, String remark, Date startDate, Date endDate) {
        // 确保 current 至少为 1
        current = Math.max(current, 1);
        size = Math.max(size, 1);

        // 计算分页偏移量
        int offset = (current - 1) * size;

        // 统计符合条件的总记录数
        int total = houseMapper.countHouses(communityId, buildingId, ownerId, floor, roomNum, roomNumber, remark, startDate, endDate);

        // 只有 total > 0 时才执行查询，避免不必要的数据库查询
        List<House> records = (total > 0) ?
                houseMapper.getHouses(communityId, buildingId, ownerId, floor, roomNum, roomNumber, remark, startDate, endDate, size, offset)
                : Collections.emptyList();

        // 返回分页数据
        return new PageResult<>(total, current, size, records);
    }


    @Override
    public List<House> listByCommunityId(Long communityId) {
        if (communityId == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "小区ID不能为空");
        }
        List<House> houses = new ArrayList<>();
        List<Building> buildingList = buildingService.listByCommunityId(communityId);
        for (Building i : buildingList){
            houses.addAll(listByBuildingId(i.getId()));
        }
        return houses;
    }

    @Override
    public List<House> listByBuildingId(Long buildingId) {
        if (buildingId == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "楼栋ID不能为空");
        }
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("building_id", buildingId);
        return list(queryWrapper);
    }

    @Override
    public boolean updateHouse(House house) {
        // 校验房屋ID是否合法
        if (house.getId() == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "房屋ID不能为空");
        }

        // 校验房屋是否存在
        House existingHouse = getById(house.getId());
        if (existingHouse == null) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "房屋信息不存在");
        }

        return updateById(house);
    }

    @Override
    public List<House> listByOwnerId(Long userId) {
        if (userId == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "业主ID不能为空");
        }
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", userId);
        return list(queryWrapper);
    }
}
